package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class reservation_delete
 */
@WebServlet("/reservation_delete")
public class reservation_delete extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public reservation_delete() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String stuid = request.getParameter("stuid");
		String reid = request.getParameter("reid");
		String delete = "delete from  reservation where reid = '" + reid + "'";
		System.out.println(delete);
		Jdbc j1 = null;

		try {
			j1 = new Jdbc(delete);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (j1.count > 0) {
			request.getRequestDispatcher("showsuccess.jsp").forward(request, response);
		}
		// 删除一行数据后进行验证，这个学生是否还有预约，有的话将 sales表中的预约改成已预约，没有的话将sales表中中预约和是否出席都改掉
		String check1 = "select*from reservation where stuid='" + stuid + "' ";
		System.out.println("=========");
		System.out.println(stuid);
		Jdbc j2 = null;
		Jdbc j3 = null;
		try {
			j2 = new Jdbc();
			j3 = new Jdbc();
			j2.Jdbc_Q2(check1);
			j2.rs.last();// 将光标移到最后一行
			j2.rs.getRow();
			System.out.println(j2.rs.getRow());
			if (j2.rs.getRow() == 0) {// 代表没有预约记录了
				String check2 = "update sales set reservation='未预约' where id='" + stuid + "' ";
				String check3 = "update sales set attend='未出席' where id ='" + stuid + "'";
				j3 = new Jdbc(check2);
				j3 = new Jdbc(check3);
			} else {// 还有预约记录，那么就将sales 更新为已预约，并且查看最新的一条预约记录是否到访
				String check4 = "update sales set reservation='已预约' where id = '" + stuid + "'";
				j3 = new Jdbc(check4);// 将数据库更新
				j2.rs.first();
				System.out.println("========查看最新的一条记录==========");
				System.out.println(j2.rs.getString("attend"));
				String attend1 = j2.rs.getString("attend");

				if (attend1.equals("") || attend1.equals("未出席") || attend1.equals("未记录")) {
					String check5 = "update sales set attend='未出席' where id = '" + stuid + "'";
					j3 = new Jdbc(check5);
				} else {
					String check6 = "update sales set attend='已出席' where id = '" + stuid + "'";
					j3 = new Jdbc(check6);
				}

			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
